using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;

public class ConfigHelper
{
    [Serializable]
    class AppsettingConfig
    {
        public float _minSpeed = 1;
        public float _maxSpeed = 2;
        public float _speedCoe = -0.002f;
        public string _launcher;
    }

    public static async Task LoadConfigAsync()
    {
        var persistentFile = Path.Combine(Application.persistentDataPath, "appsettings.ini");
        var streamingAssetsFile = Path.Combine(Application.streamingAssetsPath, "appsettings.ini");
        if (Application.platform == RuntimePlatform.Android)
        {
            if (!File.Exists(persistentFile))
            {
                var txt = ReadAssetHelper.ReadString("appsettings.ini");
                File.WriteAllText(persistentFile, txt);
            }
        }

        string path;
        if (Application.platform == RuntimePlatform.Android)
            path = persistentFile;
        else
            path = streamingAssetsFile;

        var result = await File.ReadAllTextAsync(path);
        if (string.IsNullOrEmpty(result)) return;

        var config = JsonHelper.FromJson<AppsettingConfig>(result);
        if (config == null) return;
        AppConst.MIN_SPEED = config._minSpeed;
        AppConst.MAX_SPEED = config._maxSpeed;
        AppConst.SPEED_COE = config._speedCoe;
        AppConst.launcher = config._launcher;
    }
}